<?php

namespace mpend\modules\user\models;

use Yii;
use common\logics\PositionLogic;
use common\logics\ShareLogic;
use common\models\shop\ShopOrder;
use common\models\cso\CsoUser;
use common\helpers\Url;

/**
 * 用户中心信息
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class UserCenterDashboardLogic extends \common\logics\DashboardLogic {

    /**
     * @var array 用户信息
     */
    public $user = null;

    /**
     * @var array 会员面板
     */
    public $member_panel = null;

    /**
     * @var array 申请栏
     */
    public $hurdle = null;

    /**
     * @var array 轮播图
     */
    public $broadcast = [];

    /**
     * @var array 订单badges
     */
    public $order_badges = [];

    /**
     * @var array 我的功能
     */
    public $funcs = [];

    /**
     * @var array 更多
     */
    public $others = [];

    /**
     * @var array 客服
     */
    public $custorm_service = null;

    /**
     * @var array 分享内容
     */
    public $share_data = null;

    /**
     * @var array 分享内容
     */
    private $_availdMember = false;

    /**
     * 通知消息
     * @inheritdoc
     */
    protected function setMemberPanel() {
        if (!$this->user) {
            $this->hurdle = MemberAboutLogic::promptApply();
            return null;
        }
        $userid = $this->user->id;
        $member = MemberAboutLogic::find()->active()->andWhere([
            'user_id' => $userid
        ])->one();
        if ($member) {
            $this->_availdMember = $member->isAvaild;
            $this->hurdle = $member->hurdle;
            return $member->panel;
        } else {
            $this->hurdle = MemberAboutLogic::promptApply();
        }
        return null;
    }

    /**
     * @inheritdoc
     */
    protected function setBroadcast() {
        return PositionLogic::findByName('ucenter.broadcast', []);
    }

    /**
     * @inheritdoc
     */
    protected function setOrderBadges() {
        $badges = [
            'payment' => 0,
            'deliver' => 0,
            'receive' => 0,
            'service' => 0,
        ];
        $userid = $this->user ? $this->user->id : 0;
        if ($userid) {
            $orderStats = ShopOrder::find()->active()->andWhere([
                'and',
                ['user_id' => $userid],
            ])->andWhere([
                'or',
                ['>', 'status', ShopOrder::STATUS_ACTIVE],
                [
                    'and',
                    ['status' => [ShopOrder::STATUS_DEFAULT, ShopOrder::STATUS_ACTIVE]],
                    ['>', 'created_at', time() - ShopOrder::EXPIRED_DURTION],
                ]
            ])->groupBy('status')->select([
                'status', 'count(*) as nums'
            ])->asArray()->all();

            $badgeStatus = [
                'payment' => [ShopOrder::STATUS_DEFAULT, ShopOrder::STATUS_ACTIVE],
                'deliver' => [ShopOrder::STATUS_PAID],
                'receive' => [ShopOrder::STATUS_SHIP],
                'service' => [ShopOrder::STATUS_RECEIVED, ShopOrder::STATUS_COMPLETE],
            ];

            foreach ($orderStats as $item) {
                if (in_array($item['status'], $badgeStatus['payment'])) {
                    $badges['payment'] += $item['nums'];
                } elseif (in_array($item['status'], $badgeStatus['deliver'])) {
                    $badges['deliver'] += $item['nums'];
                } elseif (in_array($item['status'], $badgeStatus['receive'])) {
                    $badges['receive'] += $item['nums'];
                }
            }
        }
        $data = [];
        foreach ($badges as $key => $num) {
            $data[] = [
                'label' => Yii::t('message', 'order_' . $key),
                'name' => $key,
                'num' => $num,
                'icon' => Url::attach('assets/icons/ucenter/' . $key . '.png'),
                'url' => [
                    'path' => '/order/list/' . $key
                ],
            ];
        }
        return $data;
    }

    /**
     * @inheritdoc
     */
    protected function setFuncs() {
        return [
            [
                'label' => '我的地址',
                'name' => 'address',
                'icon' => Url::attach('assets/icons/ucenter/address.png'),
                'url' => [
                    'path' => '/user/address'
                ],
            ],
            [
                'label' => '浏览记录',
                'name' => 'history',
                'icon' => Url::attach('assets/icons/ucenter/history.png'),
                'url' => [
                    'path' => '/user/history'
                ],
            ],
            [
                'label' => '收藏',
                'name' => 'collect',
                'icon' => Url::attach('assets/icons/ucenter/collect.png'),
                'url' => [
                    'path' => '/user/collect'
                ],
            ],
            [
                'label' => '分享',
                'name' => 'share',
                'icon' => Url::attach('assets/icons/ucenter/share.png'),
                'url' => null,
                'click' => 'onShare',
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    protected function setOthers() {
        return [
            [
                'label' => '合作咨询',
                'name' => 'cooperate',
                'icon' => Url::attach('assets/icons/ucenter/cooperate.png'),
                'url' => [
                    'path' => '/page/cooperate'
                ],
            ],
            [
                'label' => '帮助与反馈',
                'name' => 'help',
                'icon' => Url::attach('assets/icons/ucenter/help.png'),
                'url' => [
                    'path' => '/help'
                ],
            ],
            [
                'label' => '关于',
                'name' => 'about',
                'icon' => Url::attach('assets/icons/ucenter/about.png'),
                'url' => [
                    'path' => '/page/about'
                ],
            ],
            [
                'label' => '设置',
                'name' => 'setting',
                'icon' => Url::attach('assets/icons/ucenter/setting.png'),
                'url' => [
                    'path' => '/setting'
                ],
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    protected function setCustormService() {
        $userid = $this->user ? $this->user->id : 0;
        return CsoUser::findDefault($userid);
    }

    /**
     * APP应用分享信息
     * @inheritdoc
     */
    protected function setShareData() {
        $uid = $this->user ? $this->user->id : 0;
        if ($uid && $this->_availdMember) {
            return ShareLogic::spread($uid);
        }
        return ShareLogic::base($uid);
    }

}
